//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
import java.util.*;

public class first_class {
    // 中奖号码：红球(5个)和蓝球(1个)
    private static Set<Integer> winningRedBalls = new HashSet<>();
    private static int winningBlueBall;
    // 用户购买的号码组
    private static List<Map<String, Object>> userBalls = new ArrayList<>();

    public static void main(String[] args) {
        // 初始化生成中奖号码
        generateWinningNumbers();

        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("\n请选择操作：");
            System.out.println("1. 生成五组号码");
            System.out.println("2. 查看中奖结果");
            System.out.println("3. 退出程序");
            System.out.print("请输入选项(1-3)：");

            // 处理输入，防止非数字输入
            while (!scanner.hasNextInt()) {
                System.out.print("输入无效，请重新输入(1-3)：");
                scanner.next();
            }
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    generateUserNumbers();
                    break;
                case 2:
                    checkResult();
                    break;
                case 3:
                    System.out.println("程序已退出，谢谢使用！");
                    break;
                default:
                    System.out.println("输入无效，请输入1-3之间的数字！");
            }
        } while (choice != 3);

        scanner.close();
    }

    // 生成中奖号码
    private static void generateWinningNumbers() {
        // 生成5个不重复的1-33红球
        Random random = new Random();
        winningRedBalls.clear();
        while (winningRedBalls.size() < 5) {
            int red = random.nextInt(33) + 1;
            winningRedBalls.add(red);
        }
        // 生成1个1-16蓝球
        winningBlueBall = random.nextInt(16) + 1;
    }

    // 为用户生成五组号码
    private static void generateUserNumbers() {
        userBalls.clear(); // 清空之前的号码
        Random random = new Random();

        for (int i = 0; i < 5; i++) {
            // 生成一组红球
            Set<Integer> redBalls = new HashSet<>();
            while (redBalls.size() < 5) {
                int red = random.nextInt(33) + 1;
                redBalls.add(red);
            }
            // 生成蓝球
            int blueBall = random.nextInt(16) + 1;

            // 存储这组号码
            Map<String, Object> ballSet = new HashMap<>();
            ballSet.put("red", redBalls);
            ballSet.put("blue", blueBall);
            userBalls.add(ballSet);

            // 打印这组号码
            System.out.print("第" + (i + 1) + "组：红球 ");
            List<Integer> sortedReds = new ArrayList<>(redBalls);
            Collections.sort(sortedReds); // 排序后打印，方便查看
            for (int red : sortedReds) {
                System.out.printf("%02d ", red); // 两位数字格式化
            }
            System.out.printf("蓝球 %02d%n", blueBall);
        }
    }

    // 检查中奖结果
    private static void checkResult() {
        // 打印中奖号码
        System.out.print("本期中奖号码：红球 ");
        List<Integer> sortedWinningReds = new ArrayList<>(winningRedBalls);
        Collections.sort(sortedWinningReds);
        for (int red : sortedWinningReds) {
            System.out.printf("%02d ", red);
        }
        System.out.printf("蓝球 %02d%n", winningBlueBall);

        // 判断是否购买了号码
        if (userBalls.isEmpty()) {
            System.out.println("你没有购买本期双色球。");
            return;
        }

        // 检查每组号码的中奖情况
        for (int i = 0; i < userBalls.size(); i++) {
            Map<String, Object> ballSet = userBalls.get(i);
            Set<Integer> userReds = (Set<Integer>) ballSet.get("red");
            int userBlue = (int) ballSet.get("blue");

            // 计算红球匹配数量
            int redMatches = 0;
            for (int red : userReds) {
                if (winningRedBalls.contains(red)) {
                    redMatches++;
                }
            }
            // 判断蓝球是否匹配
            boolean blueMatch = (userBlue == winningBlueBall);

            // 确定中奖等级
            String result = getPrizeLevel(redMatches, blueMatch);

            // 打印该组号码及结果
            System.out.print("第" + (i + 1) + "组号码结果：");
            List<Integer> sortedUserReds = new ArrayList<>(userReds);
            Collections.sort(sortedUserReds);
            for (int red : sortedUserReds) {
                System.out.printf("%02d ", red);
            }
            System.out.printf("蓝球 %02d：", userBlue);
            System.out.println(result);
        }
    }

    // 根据匹配情况确定中奖等级
    private static String getPrizeLevel(int redMatches, boolean blueMatch) {
        if (redMatches == 5 && blueMatch) {
            return "恭喜！你中了一等奖";
        } else if (redMatches == 5 && !blueMatch) {
            return "恭喜！你中了二等奖";
        } else if (redMatches == 4 && blueMatch) {
            return "恭喜！你中了三等奖";
        } else if (redMatches == 4 || (redMatches == 3 && blueMatch)) {
            return "恭喜！你中了四等奖";
        } else if (redMatches == 3 || (redMatches == 2 && blueMatch)) {
            return "恭喜！你中了五等奖";
        } else if (blueMatch) {
            return "恭喜！你中了六等奖";
        } else {
            return "很遗憾，您未能中奖";
        }
    }
}
